package com.example.sunmiv1.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.GiftGoodsInfo;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.zxing.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GiftGoodsAdapter extends BaseAdapter {

    private ViewHolder vh;
    private Operation operation;
    private List<GiftGoodsInfo.DataBean.ListBean> listBeans;

    public GiftGoodsAdapter(List<GiftGoodsInfo.DataBean.ListBean> listBeans) {
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gift_goods_adapter, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        GiftGoodsInfo.DataBean.ListBean listBean = listBeans.get(position);
        vh.tvGiftGoodsName.setText(listBean.getGoods_name());
        vh.tvGiftGoodsSku.setText(TextUtils.isEmpty(listBean.getSku_name()) ? "" : "(" + listBean.getSku_name() + ")");
        RequestOptions options = new RequestOptions();
        options.error(R.mipmap.error);
        Glide.with(parent.getContext()).load(listBean.getGoods_image())
                .apply(options)
                .into(vh.ivGiftGoods);
        vh.tvGiftGoodsStock.setText(listBean.getRemain_inventory());
//        vh.tvGiftGoodsNum.setText(listBean.getChooseNum() + "");
        final int index = position;
        vh.linearGiftGoodsAdd.setOnClickListener(v -> {
            operation.success(1, index);
        });
        if (listBean.getChooseNum() > 0) {
            vh.giftNum.setText(listBean.getChooseNum() + "");
            vh.giftNum.setVisibility(View.VISIBLE);
        } else {
            vh.giftNum.setVisibility(View.GONE);
        }
        return convertView;
    }

    public interface Operation {
        void success(int flag, int index);//flag=1是加，flag=2是減
    }

    public void Click(Operation operation) {
        this.operation = operation;
    }

    static class ViewHolder {
        @BindView(R.id.iv_giftGoods)
        ImageView ivGiftGoods;
        @BindView(R.id.tv_giftGoods_name)
        TextView tvGiftGoodsName;
        @BindView(R.id.tv_giftGoods_stock)
        TextView tvGiftGoodsStock;
        @BindView(R.id.tv_giftGoods_sku)
        TextView tvGiftGoodsSku;
        @BindView(R.id.linear_giftGoods_add)
        RelativeLayout linearGiftGoodsAdd;
        @BindView(R.id.gift_num)
        TextView giftNum;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
